#ifndef __UDSF_I2C_SLAVE_C_
#define __UDSF_I2C_SLAVE_C_


//#include "app_def.h"

#include "./udsf_gpio.c"
#include "./udsf_delay.c"
#include "./UDSF_uart.c"
#define SLAVE_ADDR 0x2E //写发送0x50 读发送0x51
#define ADDMAX     20

#define WAIT_IIC_SCL_HIGH   udsf_i2c_timeout(1)
#define WAIT_IIC_SCL_LOW    udsf_i2c_timeout(0)


#define SCL            GPIO0_7
#define SDA            GPIO0_6
#define GET_SCL_DAT    SCL
#define GET_SDA_DAT    SDA
#define SDA_OUT        GPIO->PMODE |= (1 << 6);
#define SDA_IN         GPIO->PMODE &= ~(1 << 6);
#define SET_SDA_LOW    SDA=0;
#define SET_SDA_HIGH   SDA=1;

#define IIC_SLAVE_SEND_LOW  {if(WAIT_IIC_SCL_LOW!=0){ SDA_OUT; SET_SDA_LOW; WAIT_IIC_SCL_HIGH;}}
#define IIC_SLAVE_SEND_HIGH {WAIT_IIC_SCL_LOW; SDA_OUT; SET_SDA_HIGH; WAIT_IIC_SCL_HIGH;}

#define IIC_SLAVE_SEND_ACK  IIC_SLAVE_SEND_LOW   //从机读取结束发送ACK
#define IIC_SLAVE_SEND_NAK  IIC_SLAVE_SEND_HIGH  //从机读取结束发送NACK

#define debug  0


#define PRE_TRIGGER_TIME      0 //预触发时间 状态
#define MEASURING_TIME        1 //检测时间 状态
#define RESPOND               2 //响应状态

//#define r_w_reg       paramRSV->rsv[1]
//#define SCL_old       paramRSV->rsv[2]
//#define SDA_old       paramRSV->rsv[3]


static unsigned int udsf_i2c_timeout(unsigned char PIN) __attribute__((unused));
//static void         udsf_i2c_init(void) __attribute__((unused));
static void         udsf_i2c_salve_check_run(void) __attribute__((unused));
static void         udsf_i2c_out_init(void) __attribute__((unused));

static unsigned int udsf_i2c_timeout(unsigned char PIN)//CLK
{
    //10000->1.2ms    1000->12x us
    uint32_t time = 10000;

    if (PIN == 1)
    {
        while ((!GET_SCL_DAT) && time) //wait SCL HI
        {
            time--;
        }
    }
    else
    {
        while (GET_SCL_DAT && time)
        {
            time--;

        }
    }

    return time;
}




static void udsf_i2c_init(void)
{
//  GPIO->PMODE |= (1 << 12);//GPIO0.5 输出
//  GPIO->PMODE |= (1 << 9);//GPIO0.5 输出
//   // GPIO->PMODE = 0x00000024;
//  Write_M32(0x50010000, GPIO_PIN_9 | GPIO_PIN_12);
//        udsf_pinctrl_re((1 << 12), 1);
//        udsf_pinctrl_ue((1 << 12), 0);
//       udsf_pinctrl_re((1 << 9), 1);
//        udsf_pinctrl_ue((1 << 9), 0);

    #ifdef IIC_OUTPUT_ENABLE
    //IO 初始化
    USCI1->IMR |= 0x6;
    //    PINMUX->MUX0 = 0x00000000;

    //    udsf_pinctrl_ae(GPIO_PIN_5, 0); // 数字
    udsf_pinctrl_ae(GPIO_PIN_6, 0); // 数字
    udsf_pinctrl_ae(GPIO_PIN_7, 0); // 数字

    udsf_pinctrl_od((1 << 6), 1);
    udsf_pinctrl_od((1 << 7), 1);
    //    udsf_pinctrl_re((1 << 6), 0);
    //    udsf_pinctrl_re((1 << 7), 0);
    //    udsf_pinctrl_ue((1 << 6), 0);
    //    udsf_pinctrl_ue((1 << 7), 0);
    //输入
    //    GPIO->PMODE &= ~(1 << 6);//GPIO0.6 SDA
    //    GPIO->PMODE &= ~(1 << 7);//GPIO0.7 SCL
    // 沿中断
    GPIO->INTTYPE &= ~(1 << 6);
    // 双沿中断
    GPIO->INTPOL |= (2 << (6 << 1));
    GPIO->INTEN |= (1 << 6);
    GPIO->INTSTR |= (1 << 6);
    #endif

}

static void udsf_i2c_reg_init(void)
{
    sUDF_global.start_f = 0;
    sUDF_global.version[0] = 11;
    sUDF_global.version[1] = 1;
    sUDF_global.version[2] = 2;
    sUDF_global.version[3] = 10;
    /**/
    //寄存器初值 初始化

    sUDF_global.i2c_reg00 = 0x00;
    sUDF_global.i2c_regaa = 0xF0;

    for (int i = 0; i < 256; i++)
    {
        sUDF_global.i2c_regbb[i] = 0x00;
    }

    sUDF_global.binpage_cnt = 0;


    udsf_spiflash_read_data(&sUDF_global.i2c_reg10, IIC_Para_ADDR, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg11, IIC_Para_ADDR + 4, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg12, IIC_Para_ADDR + 8, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg13, IIC_Para_ADDR + 12, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg31, IIC_Para_ADDR + 16, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg32, IIC_Para_ADDR + 20, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg3e, IIC_Para_ADDR + 24, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg3f, IIC_Para_ADDR + 28, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg42, IIC_Para_ADDR + 32, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg43, IIC_Para_ADDR + 36, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg4e, IIC_Para_ADDR + 40, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg4f, IIC_Para_ADDR + 44, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg5c, IIC_Para_ADDR + 48, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg61, IIC_Para_ADDR + 52, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg67, IIC_Para_ADDR + 56, 1, eQSPI_FLASH_STD);
    udsf_spiflash_read_data(&sUDF_global.i2c_reg68, IIC_Para_ADDR + 60, 1, eQSPI_FLASH_STD);


    if (sUDF_global.i2c_reg10 == 0xFF)
    {
        sUDF_global.i2c_reg10 = 0x05;// 近

    }

    if (sUDF_global.i2c_reg11 == 0xFF)
    {
        sUDF_global.i2c_reg11 = 0x07;// 中触发距离

    }

    if (sUDF_global.i2c_reg12 == 0xFF)
    {
        sUDF_global.i2c_reg12 = 9;//远触发距离
        sUDF_global.i2c_reg13 = 0x00;
    }

    if (sUDF_global.i2c_reg31 == 0xFF)
    {
        sUDF_global.i2c_reg31 = 0x2C;
        sUDF_global.i2c_reg32 = 0x01;	//0x31 0x32低功耗模式帧间隔
    }

    if (sUDF_global.i2c_reg3e == 0xFF)
    {

        sUDF_global.i2c_reg3e = 0x32;
        sUDF_global.i2c_reg3f = 0x00;	//0x3e 0x3f全速模式帧间隔

    }

    sUDF_global.i2c_reg41 = 0x01;

    if (sUDF_global.i2c_reg42 == 0xFF)
    {
        sUDF_global.i2c_reg42 = 0x08;
        sUDF_global.i2c_reg43 = 0x00;  //0x42 0x43目标丢失后拉低电平所需时长
    }

    //      sUDF_global.i2c_reg44 = 0x00;
    //      sUDF_global.i2c_reg45 = 0x00;
    if (sUDF_global.i2c_reg4e == 0xFF)
    {
        sUDF_global.i2c_reg4e = 0x14;
        sUDF_global.i2c_reg4f = 0x00;	//0x4e 0x4F无目标时切换到低功耗所需周期数
    }

    if (sUDF_global.i2c_reg5c == 0xFF)
    {
        sUDF_global.i2c_reg5c = 0x07;	//0x5c 灵敏度
    }

    if (sUDF_global.i2c_reg61 == 0xFF)
    {
        sUDF_global.i2c_reg61 = 0x02;	//多帧数
    }

    if (sUDF_global.i2c_reg67 == 0xFF)
    {
				sUDF_global.i2c_reg67 = 8;
        sUDF_global.i2c_reg68 = 50;
    }

}

static void udsf_i2c_salve_check_run(void)
{
    ///*I2C slave logic*/
    //变量
    ///*I2C slave logic*/
    //变量
    unsigned char bitcount = 0, iic_slv_addr = 0, receive_reg = 0, receive_byte = 0;
    unsigned char txmask = 0x80;
    unsigned char val = 0;
    uint8_t count = 0;
    uint32_t time = 0;
//    sUDF_global.start_f = 0;
    SDA_IN

    //7ms SDA唤醒
    if (sUDF_global.state == RESPOND)
    {
        GPIO->INTPOL = (1 << (6 << 1));
        GPIO->INTSTR |= (1 << 6);
        //GPIO0_5 = 1;
		GPIO0_12 = 0;
        //开始IIC通信
        for (time = 0; time < 48000 ; time++)
        {
			
            //GPIO0_5 = 1 ;
            //检测下降沿
            if (GPIO->INTSTR & (1 << 6))
            {
                //GPIO0_5 = 1 ;
                //SCL为高电平，START信号
                if (SCL == 1)
                {

                    //接收设备地址
                    for (bitcount = 0; bitcount < 7; bitcount++)
                    {
                        if (WAIT_IIC_SCL_LOW == 0)
                        {
                            iic_slv_addr = 0;
                            break;
                        }

                        WAIT_IIC_SCL_HIGH;
                        //先移位，再读数
                        iic_slv_addr <<= 1;
                        iic_slv_addr |= GET_SDA_DAT;
                    }

                    WAIT_IIC_SCL_LOW;
                    WAIT_IIC_SCL_HIGH;
                    //                  for(bitcount = 0; bitcount < 8; bitcount++)
                    //                  {
                    //                      if(iic_slv_addr>>bitcount & 0x01){
                    //                          GPIO0_5 = 1;
                    //                      }else{
                    //                          GPIO0_5 = 0;
                    //                      }
                    //                      udsf_delay_ms(5);
                    //                  }
                    //                  GPIO0_5 = 1;

                    //设备地址正确 0x5C
                    if (iic_slv_addr == SLAVE_ADDR)
                    {

                        IIC_SLAVE_SEND_ACK;
                        //接收寄存器地址,先SDA改为输入接收1bit，之后接收剩余7bit，防止语句 SDA_IN; 多次执行
                        WAIT_IIC_SCL_LOW;
                        SDA_IN;//输入
                        WAIT_IIC_SCL_HIGH;
                        receive_reg <<= 1;
                        receive_reg |= GET_SDA_DAT;

                        //接收bit1-7
                        for (bitcount = 1; bitcount < 8; bitcount++)
                        {
                            WAIT_IIC_SCL_LOW;
                            WAIT_IIC_SCL_HIGH;
                            receive_reg <<= 1;  //先移位，再读数
                            receive_reg |= GET_SDA_DAT;
                        }

                        //寄存器地址正确，回复ACK 时间不够 只能放在前面
                        IIC_SLAVE_SEND_ACK;                   //bit9

                        //寄存器地址错误，不回复ACK
                        if ((receive_reg != 0x10) && (receive_reg != 0x11)
                                && (receive_reg != 0x31) && (receive_reg != 0x32)
                                && (receive_reg != 0x3e) && (receive_reg != 0x3f)
                                && (receive_reg != 0x41) && (receive_reg != 0x42)
                                && (receive_reg != 0x43) && (receive_reg != 0x44)
                                && (receive_reg != 0x45) && (receive_reg != 0x4e)
                                && (receive_reg != 0x4f) && (receive_reg != 0x5c)
                                && (receive_reg != 0x00) && (receive_reg != 0x0a)
                                && (receive_reg != 0x12) && (receive_reg != 0x13)
                                && (receive_reg != 0x67) && (receive_reg != 0x68)
								&& (receive_reg != 0x70) && (receive_reg != 0x71)
                                && (receive_reg != 0xaa) && (receive_reg != 0x61)
                           )
                        {
                            GPIO->INTSTR |= (1 << 6);
                            //GPIO->INTPOL = (2 << (6 << 1));
                            SDA_IN
                            return;
                        }


//                        sUDF_global.start_f = 0;

                        WAIT_IIC_SCL_LOW;
                        SDA_IN

                        WAIT_IIC_SCL_HIGH;
                        GPIO->INTSTR |= (1 << 6);
                        //锁存第一个bit 如果为写则读取后7bit组成数据，如果是读则无用
                        receive_byte <<= 1;
                        receive_byte |= GET_SDA_DAT;

                        //判断是否有restart信号 有restart为读
                        for (time = 0; time < 100; time++)
                        {

                            if ((SCL == 1) && (GPIO->INTSTR & (1 << 6))) // 读
                            {
                                //重新接收8bit寄存器地址
                                for (bitcount = 0; bitcount < 8; bitcount++)
                                {
                                    WAIT_IIC_SCL_LOW;
                                    WAIT_IIC_SCL_HIGH;
                                    receive_byte <<= 1;
                                    receive_byte |= GET_SDA_DAT;
                                }

                                //寄存器地址正确回复ACK
                                IIC_SLAVE_SEND_ACK

                                if (receive_reg == 0x10)
                                    val = sUDF_global.i2c_reg10;
                                else if (receive_reg == 0x11)
                                    val = sUDF_global.i2c_reg11;
                                else if (receive_reg == 0x12)
                                    val = sUDF_global.i2c_reg12;
                                else if (receive_reg == 0x13)
                                    val = sUDF_global.i2c_reg13;
                                else if (receive_reg == 0x31)
                                    val = sUDF_global.i2c_reg31;
                                else if (receive_reg == 0x32)
                                    val = sUDF_global.i2c_reg32;
                                else if (receive_reg == 0x3e)
                                    val = sUDF_global.i2c_reg3e;
                                else if (receive_reg == 0x3f)
                                    val = sUDF_global.i2c_reg3f;
                                else if (receive_reg == 0x5c)
                                    val = sUDF_global.i2c_reg5c;
                                else if (receive_reg == 0x00)
                                    val = sUDF_global.i2c_reg00;
                                else if (receive_reg == 0x41)
                                    val = sUDF_global.i2c_reg41;
                                else if (receive_reg == 0x42)
                                    val = sUDF_global.i2c_reg42;
                                else if (receive_reg == 0x43)
                                    val = sUDF_global.i2c_reg43;
                                //                          else if (receive_reg == 0x44)
                                //                              val = sUDF_global.i2c_reg44;
                                //                          else if (receive_reg == 0x45)
                                //                              val = sUDF_global.i2c_reg45;
                                else if (receive_reg == 0x4e)
                                    val = sUDF_global.i2c_reg4e;
                                else if (receive_reg == 0x4f)
                                    val = sUDF_global.i2c_reg4f;
                                else if (receive_reg == 0x0a)
                                    val = sUDF_global.version[count];
                                else if (receive_reg == 0x61)
                                    val = sUDF_global.i2c_reg61;
                                else if (receive_reg == 0x67)
                                    val = sUDF_global.i2c_reg67;
                                else if (receive_reg == 0x68)
                                    val = sUDF_global.i2c_reg68;
//								else if (receive_reg == 0x70)
//                                    val = sUDF_global.i2c_reg70;
//								else if (receive_reg == 0x71)
//                                    val = sUDF_global.i2c_reg71;
                                else if (receive_reg == 0xaa)
                                    val = sUDF_global.i2c_regaa;
                                else
                                {
                                    SDA_IN
                                    GPIO->INTSTR |= (1 << 6);
                                    return;
                                }


                                for (bitcount = 0; bitcount < 8; bitcount++)
                                {
                                    WAIT_IIC_SCL_LOW;

                                    if (val & (txmask >> bitcount))
                                        SDA = 1;
                                    else
                                        SDA = 0;

                                    WAIT_IIC_SCL_HIGH;
                                }

                                //等待SCL高 读取主机回复的ACK or NACK
                                WAIT_IIC_SCL_LOW;
                                //放开SDA控制
                                SDA_IN;
                                WAIT_IIC_SCL_HIGH;

                                for (count = 1; GET_SDA_DAT == 0 && receive_reg == 0x0a && count < 4; count++)
                                {
                                    val = sUDF_global.version[count];
                                    //先控制SDA发送1bit，接着循环发剩余bit，减少SDA_IN使用次数
                                    WAIT_IIC_SCL_LOW;
                                    SDA_OUT;

                                    if (val & (txmask >> 0))
                                        SDA = 1;
                                    else
                                        SDA = 0;

                                    WAIT_IIC_SCL_HIGH;

                                    for (bitcount = 1; bitcount < 8; bitcount++)
                                    {
                                        WAIT_IIC_SCL_LOW;

                                        if (val & (txmask >> bitcount))
                                            SDA = 1;
                                        else
                                            SDA = 0;

                                        WAIT_IIC_SCL_HIGH;
                                    }

                                    //等待SCL高 读取主机回复的ACK or NACK
                                    WAIT_IIC_SCL_LOW;
                                    SDA_IN;
                                    WAIT_IIC_SCL_HIGH;
                                }

                                count = 0;
                                WAIT_IIC_SCL_LOW;
//                                WAIT_IIC_SCL_HIGH;

//                                //等停止信号 退出 12us超时强制退出
//                                for (time = 0; time < 5000; time++)
//                                {
//                                    if (GET_SDA_DAT)
//                                    {

//                                        break;
//                                    }
//                                }

//                                if (time == 5000)
//                                {
//                                    GPIO->INTSTR |= (1 << 6);
//                                    //GPIO->INTPOL = (2 << (6 << 1));
//                                    SDA_IN
//                                    return;
//                                }


                                GPIO->INTSTR |= (1 << 6);
                                break;
                            }// 读结束

                            if (SCL == 0)
                            {
                                // 写

                                //接收后续7bit数据
                                for (bitcount = 1; bitcount < 8; bitcount++)
                                {
                                    WAIT_IIC_SCL_LOW;
                                    WAIT_IIC_SCL_HIGH;
                                    receive_byte <<= 1;  //先移位，再读数
                                    receive_byte |= GET_SDA_DAT;
                                }

                                IIC_SLAVE_SEND_ACK;

//                          if (receive_reg == 0x10)
//                              sUDF_global.i2c_reg10 = receive_byte;
//                          else if (receive_reg == 0x11)
//                              sUDF_global.i2c_reg11 = receive_byte;
//                          else if (receive_reg == 0x12)
//                              sUDF_global.i2c_reg12 = receive_byte;
//                          else if (receive_reg == 0x13)
//                              sUDF_global.i2c_reg13 = receive_byte;
//                          else if (receive_reg == 0x31)
//                              sUDF_global.i2c_reg31 = receive_byte;
//                          else if (receive_reg == 0x32)
//                              sUDF_global.i2c_reg32 = receive_byte;
//                          else if (receive_reg == 0x3e)
//                              sUDF_global.i2c_reg3e = receive_byte;
//                          else if (receive_reg == 0x3f)
//                              sUDF_global.i2c_reg3f = receive_byte;
//                          else if (receive_reg == 0x5c)
//                              sUDF_global.i2c_reg5c = receive_byte;
//                          else if (receive_reg == 0x00)
//                              sUDF_global.i2c_reg00 = receive_byte;
//                          else if (receive_reg == 0x41)
//                              sUDF_global.i2c_reg41 = receive_byte;
//                          else if (receive_reg == 0x42)
//                              sUDF_global.i2c_reg42 = receive_byte;
//                          else if (receive_reg == 0x43)
//                              sUDF_global.i2c_reg43 = receive_byte;
//                          else if (receive_reg == 0x44)
//                              sUDF_global.i2c_reg44 = receive_byte;
//                          else if (receive_reg == 0x45)
//                              sUDF_global.i2c_reg45 = receive_byte;
//                          else if (receive_reg == 0x4e)
//                              sUDF_global.i2c_reg4e = receive_byte;
//                          else if (receive_reg == 0x4f)
//                              sUDF_global.i2c_reg4f = receive_byte;
                                if (receive_reg == 0x00)
                                {
                                    IIC_SLAVE_SEND_ACK;
                                    GPIO->INTSTR |= (1 << 6);
                                    SDA_IN;
                                    return;
                                }
                                else if (receive_reg == 0xaa)
                                {
                                    sUDF_global.i2c_regaa = receive_byte;
                                    IIC_SLAVE_SEND_ACK;

                                    if (receive_byte == 0xee)
                                    {
                                        udsf_spiflash_erase_sector(IIC_Para_ADDR);
                                        udsf_wdt_rstnow();

                                        while (1);
                                    }

                                    if (receive_byte == 0xf1 || receive_byte == 0xf7)
                                    {
                                        SDA_IN;
                                        GPIO->INTSTR |= (1 << 6);
                                        return;
                                    }
                                }
                                else
                                {
                                    GPIO->INTSTR |= (1 << 6);
                                    return;
                                }

                                WAIT_IIC_SCL_LOW;
                                SDA_IN;
//                                WAIT_IIC_SCL_HIGH;

//                                //等停止信号 退出 12us超时强制退出
//                                for (time = 0; time < 500; time++)
//                                {
//                                    if (!GET_SDA_DAT)
//                                        break;
//                                }

//                                if ((sUDF_global.i2c_reg00 == 1) && (receive_reg == 0))
//                                {
//                                    time = 48000;
//                                    sUDF_global.i2c_reg00 = 0;
//                                }

                                GPIO->INTSTR |= (1 << 6);
                                break;
                            } //写结束

                        }//超时未读取到restart或者寄存器
                    }
                    else
                    {
                        //设备地址错误
                        GPIO->INTSTR |= (1 << 6);
                        //GPIO->INTPOL = (2 << (6 << 1));
                        return;
                    }
                }
                else
                {
                    //SCL为低
                    GPIO->INTSTR |= (1 << 6);
                    //GPIO->INTPOL = (2 << (6 << 1));
                    return;
                }
            }//检测下降沿
			WDT->STR = 1;

        }

        GPIO->INTSTR |= (1 << 6);
        //GPIO->INTPOL = (2 << (6 << 1));
    }
    else
    {
        //未7ms唤醒
    }
}


static void udsf_i2c_salve_check_run_multidata(void)
{
    ///*I2C slave logic*/
    unsigned char bitcount = 0, iic_slv_addr = 0, receive_reg = 0, receive_byte = 0;
    unsigned char txmask = 0x80;
    unsigned char val = 0;
    uint8_t count = 0;
    uint32_t time = 0;
    SDA_IN

    if (sUDF_global.state == RESPOND)
    {

        /*连续地址唤醒*/
        GPIO->INTPOL = (1 << (6 << 1));
        GPIO->INTSTR |= (1 << 6);

        for (time = 0; time < 48000 ; time++)
        {
            if (GPIO->INTSTR & (1 << 6))
            {
                //SCL为高电平，START信号
                if (SCL == 1)
                {
                    //接收设备地址
                    for (bitcount = 0; bitcount < 7; bitcount++)
                    {
                        if (WAIT_IIC_SCL_LOW == 0)
                        {
                            iic_slv_addr = 0;
                            break;
                        }

                        WAIT_IIC_SCL_HIGH;
                        //先移位，再读数
                        iic_slv_addr <<= 1;
                        iic_slv_addr |= GET_SDA_DAT;
                    }

                    WAIT_IIC_SCL_LOW;
                    WAIT_IIC_SCL_HIGH;

                    //设备地址正确 0x2E
                    if (iic_slv_addr == SLAVE_ADDR)
                    {
                        IIC_SLAVE_SEND_ACK;
                        //接收寄存器地址,先SDA改为输入接收1bit，之后接收剩余7bit，防止语句 SDA_IN; 多次执行
                        WAIT_IIC_SCL_LOW;
                        SDA_IN;//输入
                        WAIT_IIC_SCL_HIGH;
                        receive_reg <<= 1;
                        receive_reg |= GET_SDA_DAT;

                        //接收bit1-7
                        for (bitcount = 1; bitcount < 8; bitcount++)
                        {
                            WAIT_IIC_SCL_LOW;
                            WAIT_IIC_SCL_HIGH;
                            receive_reg <<= 1;  //先移位，再读数
                            receive_reg |= GET_SDA_DAT;
                        }

                        //寄存器地址正确，回复ACK 时间不够 只能放在前面
                        IIC_SLAVE_SEND_ACK;

                        //寄存器地址错误，不回复ACK，退出
                        if (  (receive_reg != 0x10)     && (receive_reg != 0x11)
                                && (receive_reg != 0x31)     && (receive_reg != 0x32)
                                && (receive_reg != 0x3E)     && (receive_reg != 0x3F)
                                && (receive_reg != 0x41)     && (receive_reg != 0x42)
                                && (receive_reg != 0x43)     && (receive_reg != 0x44)
                                && (receive_reg != 0x45)     && (receive_reg != 0x4E)
                                && (receive_reg != 0x4F)     && (receive_reg != 0x5C)
                                && (receive_reg != 0x00)     && (receive_reg != 0x67)
                                && (receive_reg != 0x68)     && (receive_reg != 0x12)
								&& (receive_reg != 0x70)     && (receive_reg != 0x71)
                                && (receive_reg != 0x13)     && (receive_reg != 0xAA)
                                && (receive_reg != 0xBB)
                                && (receive_reg != 0x0A)     && (receive_reg != 0x61))
                        {
                            GPIO->INTSTR |= (1 << 6);
                            //GPIO->INTPOL = (2 << (6 << 1));
                            SDA_IN
                            return ;
                        }

                        //开始判断读写
                        WAIT_IIC_SCL_LOW;
                        SDA_IN
                        WAIT_IIC_SCL_HIGH;
                        GPIO->INTSTR |= (1 << 6);
                        //锁存第一个bit 如果为写则读取后7bit组成数据，如果是读则无用
                        receive_byte <<= 1;
                        receive_byte |= GET_SDA_DAT;

                        //判断是否有restart信号 有restart为读
                        for (time = 0; time < 100; time++)
                        {
                            if ((SCL == 1) && (GPIO->INTSTR & (1 << 6))) // 读
                            {
                                //重新接收8bit寄存器地址
                                for (bitcount = 0; bitcount < 8; bitcount++)
                                {
                                    WAIT_IIC_SCL_LOW;
                                    WAIT_IIC_SCL_HIGH;
                                    receive_byte <<= 1;
                                    receive_byte |= GET_SDA_DAT;
                                }

                                //寄存器地址正确回复ACK
                                IIC_SLAVE_SEND_ACK

                                if (receive_reg == 0x10)
                                {
                                    val = sUDF_global.i2c_reg10;
                                }
                                else if (receive_reg == 0x11)
                                {
                                    val = sUDF_global.i2c_reg11;
                                }
                                else if (receive_reg == 0x12)
                                {
                                    val = sUDF_global.i2c_reg12;
                                }
                                else if (receive_reg == 0x13)
                                {
                                    val = sUDF_global.i2c_reg13;
                                }
                                else if (receive_reg == 0x31)
                                {
                                    val = sUDF_global.i2c_reg31;
                                }
                                else if (receive_reg == 0x32)
                                {
                                    val = sUDF_global.i2c_reg32;
                                }
                                else if (receive_reg == 0x3E)
                                {
                                    val = sUDF_global.i2c_reg3e;
                                }
                                else if (receive_reg == 0x3F)
                                {
                                    val = sUDF_global.i2c_reg3f;
                                }
                                else if (receive_reg == 0x41)
                                {
                                    val = sUDF_global.i2c_reg41;
                                }
                                else if (receive_reg == 0x42)
                                {
                                    val = sUDF_global.i2c_reg42;
                                }
                                else if (receive_reg == 0x43)
                                {
                                    val = sUDF_global.i2c_reg43;
                                }
                                else if (receive_reg == 0x4E)
                                {
                                    val = sUDF_global.i2c_reg4e;
                                }
                                else if (receive_reg == 0x4F)
                                {
                                    val = sUDF_global.i2c_reg4f;
                                }
                                else if (receive_reg == 0x5C)
                                {
                                    val = sUDF_global.i2c_reg5c;
                                }
                                else if (receive_reg == 0x00)
                                {
                                    val = sUDF_global.i2c_reg00;
                                }
                                else if (receive_reg == 0x61)
                                {
                                    val = sUDF_global.i2c_reg61;
                                }
                                else if (receive_reg == 0x67)
                                {
                                    val = sUDF_global.i2c_reg67;
                                }
                                else if (receive_reg == 0x68)
                                {
                                    val = sUDF_global.i2c_reg68;
                                }
//								else if (receive_reg == 0x70)
//                                {
//                                    val = sUDF_global.i2c_reg70;
//                                }
//								else if (receive_reg == 0x71)
//                                {
//                                    val = sUDF_global.i2c_reg71;
//                                }
                                else if (receive_reg == 0xAA)
                                {
                                    val = sUDF_global.i2c_regaa;
//									GPIO0_2 = 0;
//									GPIO0_5 = 0;
                                }
                                else if (receive_reg == 0x0A)
                                {
                                    val = sUDF_global.version[count];
                                }
                                else
                                {
                                    SDA_IN
                                    GPIO->INTSTR |= (1 << 6);
                                    return;
                                }

                                for (bitcount = 0; bitcount < 8; bitcount++)
                                {
                                    WAIT_IIC_SCL_LOW;

                                    if (val & (txmask >> bitcount))
                                        SDA = 1;
                                    else
                                        SDA = 0;

                                    WAIT_IIC_SCL_HIGH;
                                }

                                //等待SCL高 读取主机回复的ACK or NACK
                                WAIT_IIC_SCL_LOW;
                                //放开SDA控制
                                SDA_IN;
                                WAIT_IIC_SCL_HIGH;

                                for (count = 1; GET_SDA_DAT == 0 && receive_reg == 0x0a && count < 4; count++)
                                {
                                    val = sUDF_global.version[count];
                                    //先控制SDA发送1bit，接着循环发剩余bit，减少SDA_IN使用次数
                                    WAIT_IIC_SCL_LOW;
                                    SDA_OUT;

                                    if (val & (txmask >> 0))
                                        SDA = 1;
                                    else
                                        SDA = 0;

                                    WAIT_IIC_SCL_HIGH;

                                    for (bitcount = 1; bitcount < 8; bitcount++)
                                    {
                                        WAIT_IIC_SCL_LOW;

                                        if (val & (txmask >> bitcount))
                                            SDA = 1;
                                        else
                                            SDA = 0;

                                        WAIT_IIC_SCL_HIGH;
                                    }

                                    //等待SCL高 读取主机回复的ACK or NACK
                                    WAIT_IIC_SCL_LOW;
                                    SDA_IN;
                                    WAIT_IIC_SCL_HIGH;
                                }

                                count = 0;
                                WAIT_IIC_SCL_LOW;
//                                WAIT_IIC_SCL_HIGH;

//                                //等停止信号 退出 12us超时强制退出
//                                for (time = 0; time < 5000; time++)
//                                {
//                                    if (GET_SDA_DAT)
//                                    {
//                                        break;
//                                    }
//                                }

//                                if (time == 5000)
//                                {
//                                    GPIO->INTSTR |= (1 << 6);
//                                    //GPIO->INTPOL = (2 << (6 << 1));
//                                    SDA_IN
//                                    return;
//                                }

                                //time = 0;
                                GPIO->INTSTR |= (1 << 6);
                                break;

                            }
                            else if (SCL == 0)// 写
                            {
                                //接收后续7bit数据
                                for (bitcount = 1; bitcount < 8; bitcount++)
                                {
                                    WAIT_IIC_SCL_LOW;
                                    WAIT_IIC_SCL_HIGH;
                                    receive_byte <<= 1;  //先移位，再读数
                                    receive_byte |= GET_SDA_DAT;
                                }

                                IIC_SLAVE_SEND_ACK;

                                if (receive_reg == 0x10)
                                {
                                    sUDF_global.i2c_reg10 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg10,IIC_Para_ADDR,1);
                                }

                                else if (receive_reg == 0x11)
                                {
                                    sUDF_global.i2c_reg11 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg11,IIC_Para_ADDR+4,1);
                                }
                                else if (receive_reg == 0x12)
                                {
                                    sUDF_global.i2c_reg12 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg12,IIC_Para_ADDR+8,1);
                                }
                                else if (receive_reg == 0x13)
                                {
                                    sUDF_global.i2c_reg13 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg13,IIC_Para_ADDR+12,1);
                                }
                                else if (receive_reg == 0x31)
                                {
                                    sUDF_global.i2c_reg31 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg31,IIC_Para_ADDR+16,1);
                                }
                                else if (receive_reg == 0x32)
                                {
                                    sUDF_global.i2c_reg32 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg32,IIC_Para_ADDR+20,1);
                                }
                                else if (receive_reg == 0x3e)
                                {
                                    sUDF_global.i2c_reg3e = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg3e,IIC_Para_ADDR+24,1);
                                }
                                else if (receive_reg == 0x3f)
                                {
                                    sUDF_global.i2c_reg3f = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg3f,IIC_Para_ADDR+28,1);
                                }
//                      else if (r_w_reg == 0x41){
//                          sUDF_global.i2c_reg41 = receive_byte;
//                          IIC_SLAVE_SEND_ACK;
//                      }
                                else if (receive_reg == 0x42)
                                {
                                    sUDF_global.i2c_reg42 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg42,IIC_Para_ADDR+32,1);
                                }
                                else if (receive_reg == 0x43)
                                {
                                    sUDF_global.i2c_reg43 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg43,IIC_Para_ADDR+36,1);
                                }

                                else if (receive_reg == 0x4e)
                                {
                                    sUDF_global.i2c_reg4e = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg4e,IIC_Para_ADDR+40,1);
                                }
                                else if (receive_reg == 0x4f)
                                {
                                    sUDF_global.i2c_reg4f = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg4f,IIC_Para_ADDR+44,1);
                                }
                                else if (receive_reg == 0x5c)
                                {
                                    sUDF_global.i2c_reg5c = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                    //udsf_spiflash_write_data(&sUDF_global.i2c_reg5c,IIC_Para_ADDR+48,1);
                                }
                                else if (receive_reg == 0x00)
                                {
                                    sUDF_global.i2c_reg00 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                }

                                //            else if (r_w_reg == 0x76){
                                //                          sUDF_global.i2c_reg76 = receive_byte;
                                //                          IIC_SLAVE_SEND_ACK;
                                //              udsf_spiflash_write_data(&sUDF_global.i2c_reg76,PIN_Para_ADDR+20,1);
                                //                      }
                                else if (receive_reg == 0x61)
                                {
                                    sUDF_global.i2c_reg61 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                }
                                else if (receive_reg == 0x67)
                                {
                                    sUDF_global.i2c_reg67 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                }
                                else if (receive_reg == 0x68)
                                {
                                    sUDF_global.i2c_reg68 = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;
                                }
//								else if (receive_reg == 0x70)
//                                {
//                                    sUDF_global.i2c_reg70 = receive_byte;
//                                    //IIC_SLAVE_SEND_ACK;
//                                }
//								else if (receive_reg == 0x71)
//                                {
//                                    sUDF_global.i2c_reg71 = receive_byte;
//                                    //IIC_SLAVE_SEND_ACK;
//                                }
                                else if (receive_reg == 0xaa)
                                {
                                    sUDF_global.i2c_regaa = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;

                                    if (receive_byte == 0xee)
                                    {
                                        udsf_spiflash_erase_sector(IIC_Para_ADDR);
                                        udsf_wdt_rstnow();
                                        //WDT_RSTNOW();

                                        while (1);
                                    }

                                    //0xF2 确认写入，0xF3 则重新开始 OTA 升级,0xF4 则退出 OTA 升级
                                    if (receive_byte == 0xf2 || receive_byte == 0xf3 || receive_byte == 0xf4)
                                    {
//                                        WAIT_IIC_SCL_LOW;
                                        //IIC_SLAVE_SEND_ACK
                                        GPIO->INTSTR |= (1 << 6);
                                        SDA_IN;
                                        return;
                                    }
                                }
                                else if (receive_reg == 0xbb  && sUDF_global.i2c_regaa != 0xF0)
                                {

                                    uint8_t i = 0;
                                    sUDF_global.i2c_regbb[i] = receive_byte;
                                    //IIC_SLAVE_SEND_ACK;

                                    for ( i = 1; i < 128; i++)
                                    {
                                        receive_byte = 0;

                                        WAIT_IIC_SCL_LOW;
                                        SDA_IN;//输入
                                        WAIT_IIC_SCL_HIGH;
                                        receive_byte <<= 1;
                                        receive_byte |= GET_SDA_DAT;

                                        //接收bit1-7
                                        for (bitcount = 1; bitcount < 8; bitcount++)
                                        {
                                            if(WAIT_IIC_SCL_LOW == 0)
                                            {
                                                //超时雷达重启
                                                udsf_wdt_rstnow();
                                            }
//											WAIT_IIC_SCL_LOW;
                                            WAIT_IIC_SCL_HIGH;
                                            receive_byte <<= 1;  //先移位，再读数
                                            receive_byte |= GET_SDA_DAT;
                                        }
                                        sUDF_global.i2c_regbb[i] = receive_byte;
										IIC_SLAVE_SEND_ACK;
										//GPIO0_5 = !GPIO0_5;
                                        //sUDF_global.i2c_regaa = sUDF_global.binpage_cnt;
                                    }
                                    sUDF_global.binpage_cnt++;
                                    GPIO->INTSTR |= (1 << 6);
                                    SDA_IN
                                    return;
                                }
                                else
                                {
                                    GPIO->INTSTR |= (1 << 6);
                                    SDA_IN;
                                    return;
                                }

                                WAIT_IIC_SCL_LOW;
                                SDA_IN;
//                                WAIT_IIC_SCL_HIGH;

//                                //等停止信号 退出 12us超时强制退出
//                                for (time = 0; time < 500; time++)
//                                {
//                                    if (!GET_SDA_DAT)
//                                        break;
//                                }

                                if ((sUDF_global.i2c_reg00 == 1) && (receive_reg == 0))
                                {
                                    time = 48000;
                                    //sUDF_global.i2c_reg00 = receive_reg;
                                    udsf_spiflash_erase_sector(IIC_Para_ADDR);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg10, IIC_Para_ADDR, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg11, IIC_Para_ADDR + 4, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg12, IIC_Para_ADDR + 8, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg13, IIC_Para_ADDR + 12, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg31, IIC_Para_ADDR + 16, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg32, IIC_Para_ADDR + 20, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg3e, IIC_Para_ADDR + 24, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg3f, IIC_Para_ADDR + 28, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg42, IIC_Para_ADDR + 32, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg43, IIC_Para_ADDR + 36, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg4e, IIC_Para_ADDR + 40, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg4f, IIC_Para_ADDR + 44, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg5c, IIC_Para_ADDR + 48, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg61, IIC_Para_ADDR + 52, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg67, IIC_Para_ADDR + 56, 1);
                                    udsf_spiflash_write_data(&sUDF_global.i2c_reg68, IIC_Para_ADDR + 60, 1);

                                    return;
                                }

                                GPIO->INTSTR |= (1 << 6);
                                break;

                            }
                        }//超时未读取到restart或者寄存器

                    }
                    else//设备地址错误
                    {

                        GPIO->INTSTR |= (1 << 6);
                        //GPIO->INTPOL = (2 << (6 << 1));
                        return;
                    }
                }
                else//SDA由高到低 SCL为低，不是开始信号
                {
                    GPIO->INTSTR |= (1 << 6);
                    //GPIO->INTPOL = (2 << (6 << 1));
                    return;
                }

            }//检测下降沿
        }

        GPIO->INTSTR |= (1 << 6);
    }
    else
    {
        //没发现下降沿，继续执行正常代码
    }

}
#endif
